import { ElMessage } from 'element-plus';
import ClipboardJS from 'clipboard';
export function fallbackCopy(text: string) {
  const textarea = document.createElement('textarea');
  textarea.style.position = 'fixed';
  textarea.style.width = '2em';
  textarea.style.height = '2em';
  textarea.style.border = 'none';
  textarea.style.outline = 'none';
  textarea.style.boxShadow = 'none';
  textarea.style.background = 'transparent';
  document.body.appendChild(textarea);
  textarea.value = text;
  textarea.select();

  const clipboard = new ClipboardJS('.copy-btn', {
    text: () => text
  });

  clipboard.on('success', e => {
    console.log('复制成功', e);
    ElMessage({
      type: 'success',
      message: '复制成功'
    });
    clipboard.destroy();
    document.body.removeChild(textarea);
  });

  clipboard.on('error', e => {
    console.error('复制失败', e);
    ElMessage({
      type: 'error',
      message: '复制失败，请重试'
    });
    clipboard.destroy();
    document.body.removeChild(textarea);
  });

  // 触发点击事件
  const button = document.createElement('button');
  button.className = 'copy-btn';
  document.body.appendChild(button);
  button.click();
  document.body.removeChild(button);
}
